<?php namespace App\Http\Services;

class ToolsManager
{
    public static function ASCII($params = array()){
        if(!empty($params)){
           $p =  ksort($params);
           if($p){
               $str = '';
               foreach ($params as $k=>$val){
//                   $str .= $k .'=' . $val . '&';
                   $str .= $k . $val;
               }
                $strs = rtrim($str, '&');
                return $strs;
            }
        }
        return false;
    }
    /**
     * 生成订单编号
     * @return string
     */
    public static function makeBillNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 999));
        return $orderSn;
    }

    /**
     * 生城提取码
     * @return string
     */
    public static function makePickupCode(){
        $pickupCode = sprintf('%06d', rand(000000,999999));

        return $pickupCode;
    }

    /**
     * base64图片转化
     * @param $img_file
     * @return string
     */
    public static function base64EncodeImage($img_file) {
//        $base64_image = '';
//        $image_info = getimagesize($image_file);
        $img_base64 = '';
        if (file_exists($img_file)) {
            $app_img_file = $img_file; // 图片路径
            $img_info = getimagesize($app_img_file); // 取得图片的大小，类型等

            //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
            $fp = fopen($app_img_file, "r"); // 图片是否可读权限

            if ($fp) {
                $filesize = filesize($app_img_file);
                $content = fread($fp, $filesize);
                $file_content = chunk_split(base64_encode($content)); // base64编码
                switch ($img_info[2]) {           //判读图片类型
                    case 1: $img_type = "gif";
                        break;
                    case 2: $img_type = "jpg";
                        break;
                    case 3: $img_type = "png";
                        break;
                }

                $img_base64 = $file_content;//合成图片的base64编码

            }
            fclose($fp);
        }

        return $img_base64; //返回图片的base64
    }
}